from django.contrib.sites.shortcuts import get_current_site
from django.db import models
from django.urls import reverse


class User(models.Model):
    """
    用户信息表
    """
    uid = models.CharField('用户ID', max_length=32, primary_key=True)
    email = models.CharField('电子邮件', max_length=32, unique=True, blank=True)
    name = models.CharField('昵称', max_length=100)
    sex = models.CharField('性别', max_length=2)
    password = models.CharField('密码', max_length=32)
    avatar = models.FileField(upload_to='avatars/', default="/avatars/default_user.png")  # 上传文件的字段
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('修改时间', auto_now_add=True)
    source = models.CharField("创建来源", max_length=100)

    def get_absolute_url(self):
        return reverse(
            'blog:user_detail', kwargs={
                'user_name': self.username})

    def __unicode__(self):
        return self.email

    def get_full_url(self):
        site = get_current_site().domain
        url = "https://{site}{path}".format(site=site,
                                            path=self.get_absolute_url())
        return url

    class Meta:
        db_table = 'blog_user'
        ordering = ['-update_time']
        verbose_name = "用户信息表"
        verbose_name_plural = verbose_name
        get_latest_by = 'update_time'

